/*
    TwoPageView image viewer
    Copyright (C) 2006 Japan Manoretimedia Lab.

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace TwoPageView
{
    public partial class FormUserPreference : Form
    {
        private const int THUMBNAIL_SIZE_MAX = 640;
        private const int THUMBNAIL_SIZE_MIN = 1;

        private bool bThumbnailMustUpdate = false;
        private UserPreference userPreference;
        private System.Resources.ResourceManager rm;
        private string helpFileName;
        private string[] shortcutKeys;
        private static System.Windows.Forms.FolderBrowserDialog fbd = new FolderBrowserDialog();

        public bool ThumbnailMustUpdate
        {
            get { return bThumbnailMustUpdate; }
        }

        private enum InterpolationMenuItemEnum
        {
            NearestNeighbor,
            Low,
            Bilinear,
            Bicubic,
            HighQualityBilinear,

            HighQualityBicubic,
            High,
        }

        public FormUserPreference(UserPreference userPreference, string helpFileName)
        {
            InitializeComponent();
            this.userPreference = userPreference;
            this.rm = Properties.Resources.ResourceManager;
            this.helpFileName = helpFileName;
            shortcutKeys = new string[(int)ShortcutKey.Max];
            UserPreferenceToForm(userPreference);
        }

        private void UserPreferenceToForm(UserPreference up)
        {
            checkBoxFullScreen.Checked = up.FullScreen;
            checkBoxOpenFolder.Checked = up.OpenFolderWhenStartup;
            textBoxOpenFolderPath.Text = up.OpenFolderPath;
            checkBoxUseTmpDir.Checked = up.UseSpecifiedTmpFolder;
            textBoxTmpDirPath.Text    = up.TmpFolderPath;
            DirectoryOrZipDirInfo.SetPrefixOfTmpFolderPath(up.TmpFolderPath, up.UseSpecifiedTmpFolder);
            textBoxThumbnailSize.Text = string.Format(System.Globalization.CultureInfo.InvariantCulture, "{0}", up.ThumbnailSize);
            textBoxSlideshowAdvanceMSec.Text = string.Format(System.Globalization.CultureInfo.InvariantCulture, "{0}", up.SlideshowAdvanceMSec);
            if (checkBoxOpenFolder.Checked) {
                textBoxOpenFolderPath.Enabled = true;
                buttonBrowse.Enabled = true;
            } else {
                textBoxOpenFolderPath.Enabled = false;
                buttonBrowse.Enabled = false;
            }
            radioButtonRightToLeft.Checked    = DocumentArrangeDirection.RightToLeft == up.DocumentArrangeDirectionProperty;
            radioButtonLeftToRight.Checked    = DocumentArrangeDirection.LeftToRight == up.DocumentArrangeDirectionProperty;
            pictureBoxBGColor.BackColor       = up.BackColor.Color();
            radioButtonPageAutoSelect.Checked         = PageViewModeSelection.AutoSelect == up.PageViewModeSelectionProperty;
            radioButtonPageAlways1.Checked            = PageViewModeSelection.Always1Page == up.PageViewModeSelectionProperty;
            radioButtonPageAlways1_Horizontal.Checked = PageViewModeSelection.Always1PageHorizontal == up.PageViewModeSelectionProperty;
            radioButtonPageAlways1_Horizontal270.Checked = PageViewModeSelection.Always1PageHorizontal270 == up.PageViewModeSelectionProperty;
            radioButtonPageAlways1_Vertical.Checked = PageViewModeSelection.Always1PageVertical == up.PageViewModeSelectionProperty;
            radioButtonPageAlways1_Vertical270.Checked = PageViewModeSelection.Always1PageVertical270 == up.PageViewModeSelectionProperty;
            checkBoxPreloadAllImagesInFolder.Checked = up.PreloadAllImageInTheFolder;
            checkBoxDisplayStatusBar.Checked          = up.DisplayStatusBar;
            switch (up.InterpolationModeProperty) {
            case System.Drawing.Drawing2D.InterpolationMode.Bilinear:
            default:
                comboBoxScalingAlgorithm.SelectedIndex = (int)InterpolationMenuItemEnum.Bilinear;
                break;
            case System.Drawing.Drawing2D.InterpolationMode.Bicubic:
                comboBoxScalingAlgorithm.SelectedIndex = (int)InterpolationMenuItemEnum.Bicubic;
                break;
            case System.Drawing.Drawing2D.InterpolationMode.High:
                comboBoxScalingAlgorithm.SelectedIndex = (int)InterpolationMenuItemEnum.High;
                break;
            case System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic:
                comboBoxScalingAlgorithm.SelectedIndex = (int)InterpolationMenuItemEnum.HighQualityBicubic;
                break;
            case System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear:
                comboBoxScalingAlgorithm.SelectedIndex = (int)InterpolationMenuItemEnum.HighQualityBilinear;
                break;
            case System.Drawing.Drawing2D.InterpolationMode.Low:
                comboBoxScalingAlgorithm.SelectedIndex = (int)InterpolationMenuItemEnum.Low;
                break;
            case System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor:
                comboBoxScalingAlgorithm.SelectedIndex = (int)InterpolationMenuItemEnum.NearestNeighbor;
                break;
            }
            switch (up.ThumbnailInterpolationModeProperty) {
            case System.Drawing.Drawing2D.InterpolationMode.Bilinear:
            default:
                comboBoxThumbnailScalingAlgorithm.SelectedIndex = (int)InterpolationMenuItemEnum.Bilinear;
                break;
            case System.Drawing.Drawing2D.InterpolationMode.Bicubic:
                comboBoxThumbnailScalingAlgorithm.SelectedIndex = (int)InterpolationMenuItemEnum.Bicubic;
                break;
            case System.Drawing.Drawing2D.InterpolationMode.High:
                comboBoxThumbnailScalingAlgorithm.SelectedIndex = (int)InterpolationMenuItemEnum.High;
                break;
            case System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic:
                comboBoxThumbnailScalingAlgorithm.SelectedIndex = (int)InterpolationMenuItemEnum.HighQualityBicubic;
                break;
            case System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear:
                comboBoxThumbnailScalingAlgorithm.SelectedIndex = (int)InterpolationMenuItemEnum.HighQualityBilinear;
                break;
            case System.Drawing.Drawing2D.InterpolationMode.Low:
                comboBoxThumbnailScalingAlgorithm.SelectedIndex = (int)InterpolationMenuItemEnum.Low;
                break;
            case System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor:
                comboBoxThumbnailScalingAlgorithm.SelectedIndex = (int)InterpolationMenuItemEnum.NearestNeighbor;
                break;
            }
            for (int i = 0; i < shortcutKeys.Length; ++i) {
                shortcutKeys[i] = up.ShortcutKeyDisplayStringGet((ShortcutKey)i);
            }
            ListBoxKeyboardShortcutUpdate();
        }

        private void ListBoxKeyboardShortcutUpdate()
        {
            listBoxKeyboardShortcuts.Items.Clear();
            for (int i = 0; i < (int)ShortcutKey.Max; ++i) {
                string s = string.Format(System.Globalization.CultureInfo.InvariantCulture,
                    "{0} : {1}", rm.GetString(((ShortcutKey)i).ToString()),
                    (0 < shortcutKeys[i].Length) ?
                    shortcutKeys[i]:
                    rm.GetString("SK_None"));
                listBoxKeyboardShortcuts.Items.Add(s);
            }
        }

        private void FormUserPreference_Load(object sender, EventArgs e)
        {
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            userPreference.FullScreen = checkBoxFullScreen.Checked;
            userPreference.OpenFolderWhenStartup = checkBoxOpenFolder.Checked;
            userPreference.OpenFolderPath = textBoxOpenFolderPath.Text;
            userPreference.UseSpecifiedTmpFolder = checkBoxUseTmpDir.Checked;
            userPreference.TmpFolderPath = textBoxTmpDirPath.Text;
            DirectoryOrZipDirInfo.SetPrefixOfTmpFolderPath(userPreference.TmpFolderPath, userPreference.UseSpecifiedTmpFolder);
            userPreference.DocumentArrangeDirectionProperty = (radioButtonRightToLeft.Checked) ? 
                DocumentArrangeDirection.RightToLeft : 
                DocumentArrangeDirection.LeftToRight;

            PageViewModeSelection pageViewMode = PageViewModeSelection.AutoSelect;
            if (radioButtonPageAlways1.Checked) {
                pageViewMode = PageViewModeSelection.Always1Page;
            } else if (radioButtonPageAlways1_Horizontal.Checked) {
                pageViewMode = PageViewModeSelection.Always1PageHorizontal;
            } else if (radioButtonPageAlways1_Horizontal270.Checked) {
                pageViewMode = PageViewModeSelection.Always1PageHorizontal270;
            } else if (radioButtonPageAlways1_Vertical.Checked) {
                pageViewMode = PageViewModeSelection.Always1PageVertical;
            } else if (radioButtonPageAlways1_Vertical270.Checked) {
                pageViewMode = PageViewModeSelection.Always1PageVertical270;
            }
            if (userPreference.PageViewModeSelectionProperty != pageViewMode) {
                userPreference.PageViewModeSelectionProperty = pageViewMode;
                bThumbnailMustUpdate = true;
            }

            if (userPreference.PreloadAllImageInTheFolder != checkBoxPreloadAllImagesInFolder.Checked) {
                userPreference.PreloadAllImageInTheFolder = checkBoxPreloadAllImagesInFolder.Checked;
                bThumbnailMustUpdate = true;
            }
            userPreference.DisplayStatusBar = checkBoxDisplayStatusBar.Checked;
            userPreference.BackColorSet(pictureBoxBGColor.BackColor);

            for (int i = 0; i < shortcutKeys.Length; ++i) {
                shortcutKeys[i] = UserPreference.ShortcutKeyDisplayStringToShortcutKeyString(shortcutKeys[i]);
            }
            userPreference.ShortcutKeys = shortcutKeys;

            try {
                int thumbnailSize = System.Convert.ToInt32(textBoxThumbnailSize.Text, System.Globalization.CultureInfo.InvariantCulture);
                if (thumbnailSize < THUMBNAIL_SIZE_MIN) {
                    thumbnailSize = THUMBNAIL_SIZE_MIN;
                }
                if (THUMBNAIL_SIZE_MAX < thumbnailSize) {
                    thumbnailSize = THUMBNAIL_SIZE_MAX;
                }
                if (userPreference.ThumbnailSize != thumbnailSize) {
                    userPreference.ThumbnailSize = thumbnailSize;
                    if (!userPreference.PreloadAllImageInTheFolder) {
                        bThumbnailMustUpdate = true;
                    }
                }
            } catch (System.FormatException ex)
            {
                Console.WriteLine(ex.ToString());
            }
            try {
                userPreference.SetSlideshowAdvanceMSec(System.Convert.ToInt32(textBoxSlideshowAdvanceMSec.Text, System.Globalization.CultureInfo.InvariantCulture));
            } catch (System.FormatException ex) {
                Console.WriteLine(ex.ToString());
            }

            System.Drawing.Drawing2D.InterpolationMode interpolationMode = System.Drawing.Drawing2D.InterpolationMode.Bilinear;
            switch (comboBoxScalingAlgorithm.SelectedIndex) {
            case (int)InterpolationMenuItemEnum.Bilinear:
            default:
                interpolationMode = System.Drawing.Drawing2D.InterpolationMode.Bilinear;
                break;
            case (int)InterpolationMenuItemEnum.Bicubic:
                interpolationMode = System.Drawing.Drawing2D.InterpolationMode.Bicubic;
                break;
            case (int)InterpolationMenuItemEnum.High:
                interpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
                break;
            case (int)InterpolationMenuItemEnum.HighQualityBicubic:
                interpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                break;
            case (int)InterpolationMenuItemEnum.HighQualityBilinear:
                interpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear;
                break;
            case (int)InterpolationMenuItemEnum.Low:
                interpolationMode = System.Drawing.Drawing2D.InterpolationMode.Low;
                break;
            case (int)InterpolationMenuItemEnum.NearestNeighbor:
                interpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
                break;
            }
            if (userPreference.InterpolationModeProperty != interpolationMode) {
                userPreference.InterpolationModeProperty = interpolationMode;
            }

            interpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
            switch (comboBoxThumbnailScalingAlgorithm.SelectedIndex) {
            case (int)InterpolationMenuItemEnum.Bilinear:
                interpolationMode = System.Drawing.Drawing2D.InterpolationMode.Bilinear;
                break;
            case (int)InterpolationMenuItemEnum.Bicubic:
                interpolationMode = System.Drawing.Drawing2D.InterpolationMode.Bicubic;
                break;
            case (int)InterpolationMenuItemEnum.High:
                interpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
                break;
            case (int)InterpolationMenuItemEnum.HighQualityBicubic:
            default:
                interpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                break;
            case (int)InterpolationMenuItemEnum.HighQualityBilinear:
                interpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear;
                break;
            case (int)InterpolationMenuItemEnum.Low:
                interpolationMode = System.Drawing.Drawing2D.InterpolationMode.Low;
                break;
            case (int)InterpolationMenuItemEnum.NearestNeighbor:
                interpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
                break;
            }
            if (userPreference.ThumbnailInterpolationModeProperty != interpolationMode) {
                userPreference.ThumbnailInterpolationModeProperty = interpolationMode;
                bThumbnailMustUpdate = true;
            }
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void checkBoxOpenFolder_Click(object sender, EventArgs e)
        {
            if (checkBoxOpenFolder.Checked) {
                textBoxOpenFolderPath.Enabled = true;
                buttonBrowse.Enabled = true;
            } else {
                textBoxOpenFolderPath.Enabled = false;
                buttonBrowse.Enabled = false;
            }
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void buttonBrowse_Click(object sender, EventArgs e)
        {
            fbd.Description = rm.GetString("SpecifyImageFolder");
            System.Windows.Forms.DialogResult dr = fbd.ShowDialog();
            if (dr != System.Windows.Forms.DialogResult.OK) {
                return;
            }
            textBoxOpenFolderPath.Text = fbd.SelectedPath;
        }

        private void listBoxKeyboardShortcuts_Click(object sender, EventArgs e)
        {
            if (listBoxKeyboardShortcuts.SelectedIndex < 0 ||
                (int)ShortcutKey.Max <= listBoxKeyboardShortcuts.SelectedIndex) {
                return;
            }
            textBoxKBShortcut.Text = shortcutKeys[listBoxKeyboardShortcuts.SelectedIndex];
            textBoxKBShortcut.Focus();
        }

        private void textBoxKBShortcut_KeyUp(object sender, KeyEventArgs e)
        {
            int n = listBoxKeyboardShortcuts.SelectedIndex;
            if (n < 0 || (int)ShortcutKey.Max <= n) {
                return;
            }
            shortcutKeys[n] = UserPreference.ShortcutKeyStringToShortcutKeyDisplayString(e.KeyCode.ToString());
            textBoxKBShortcut.Text = shortcutKeys[n];
            ListBoxKeyboardShortcutUpdate();
            listBoxKeyboardShortcuts.SelectedIndex = n;
        }

        private void buttonChangeBGColor_Click(object sender, EventArgs e)
        {
            using (System.Windows.Forms.ColorDialog cd = new System.Windows.Forms.ColorDialog()) {
                cd.ShowHelp = true;
                cd.Color = userPreference.BackColor.Color();
                if (DialogResult.OK == cd.ShowDialog()) {
                    pictureBoxBGColor.BackColor = cd.Color;
                }
            }
        }

        private void pictureBoxBGColor_Click(object sender, EventArgs e)
        {
            buttonChangeBGColor_Click(sender, e);
        }

        private void buttonHelp_Click(object sender, EventArgs e)
        {
            System.Windows.Forms.Help.ShowHelp(this, helpFileName, string.Format(System.Globalization.CultureInfo.InvariantCulture, "{0}::/customize.htm", helpFileName));
        }

        private void buttonReset_Click(object sender, EventArgs e)
        {
            UserPreference up = new UserPreference();
            UserPreferenceToForm(up);
        }

        private void buttonBrowseTmpDir_Click(object sender, EventArgs e)
        {
            fbd.Description = rm.GetString("SpecifyTmpFolder");
            System.Windows.Forms.DialogResult dr = fbd.ShowDialog();
            if (dr != System.Windows.Forms.DialogResult.OK)
            {
                return;
            }
            textBoxTmpDirPath.Text = fbd.SelectedPath;
        }

        private void checkBoxTmpDir_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBoxUseTmpDir.Checked)
            {
                textBoxTmpDirPath.Enabled = true;
                buttonBrowseTmpDir.Enabled = true;
            }
            else
            {
                textBoxTmpDirPath.Enabled = false;
                buttonBrowseTmpDir.Enabled = false;
            }
        }
    }
}